<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class kartupiutang extends CI_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->library('auth');
        $this->load->library('base');
        $this->load->library('ajaxresponse');
        $this->load->library("phpexcel");
        $this->load->library("PHPExcel/iofactory");

        $this->load->model("report/m_kartupiutang");
        $this->load->model("crud/m_info_usaha");
    }

    function index() {
        $this->kartupiutang();
    }

    function kartupiutang(){
        $this->auth->is_logged_in('form/login');
        $this->base->head_html('Kartu Piutang Anggota');
        $data["info_usaha"] = $this->m_info_usaha->get()->result();
        $this->load->view('pengurus/br-08/kartupiutang', $data);
    }

    function calc_kartupiutang(){
        $date = $this->input->post("TGL_PIUTANG");
        $result = $this->m_kartupiutang->sum_piutang($date);

        // --------------------------- create excel ---------------------------------//
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("Kartu Piutang Anggota")->setDescription("description");

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(false);
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);

        $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(1);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.75);
        $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(1);

        $objPHPExcel->getActiveSheet()->getStyle('A8')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THICK);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A8')->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('A8'), 'B8:F8' );

        $objPHPExcel->getActiveSheet()->setCellValue('A1', "KOPERASI KARYAWAN TOTAL INDONESIE");
        $objPHPExcel->getActiveSheet()->setCellValue('A2', "JL.JEND.SUDIRMAN H2 NO.5-6 BALIKPAPAN PERMAI");
        $objPHPExcel->getActiveSheet()->setCellValue('A3', "BALIKPAPAN");
        $objPHPExcel->getActiveSheet()->setCellValue('A4', "Tlp.: 0542-427723, Fac.: 0542-427722");
        $sheet = $objPHPExcel->getActiveSheet();
        $sheet->mergeCells('A1:F1');
        $sheet->mergeCells('A2:F2');
        $sheet->mergeCells('A3:F3');
        $sheet->mergeCells('A4:F4');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A3')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle('A4')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);



        $objPHPExcel->getActiveSheet()->setCellValue('A6', "JURNAL BULAN : Oktober 2010");
        $objPHPExcel->getActiveSheet()->setCellValue('F6', "TANGGAL : 10-02-2012");


        $objPHPExcel->getActiveSheet()->setCellValue('A8', "No");
        $objPHPExcel->getActiveSheet()->setCellValue('B8', "No.Anggota");
        $objPHPExcel->getActiveSheet()->setCellValue('C8', "Nama Anggota");
        $objPHPExcel->getActiveSheet()->setCellValue('D8', "Jenis Kredit");
        $objPHPExcel->getActiveSheet()->setCellValue('E8', "Jumlah");
        $objPHPExcel->getActiveSheet()->setCellValue('F8', "Sub Total");

        $i = 8;
        $id = 0;
        $no = 0;
        $totalpiutang = 0;
        foreach($result->result() as $row) {
                $i++;
                if ($id!=$row->ID_ANGGOTA || $id==0){
                    $no += 1;
                    $id = $row->ID_ANGGOTA;
                    $nama = $row->NAMA_ANGGOTA;
                    $uraian = $row->URAIAN_USAHA;
                    $piutang = $row->PIUTANG;
                    $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $no);
                    $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $id);
                    $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $nama);
                    $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $uraian);
                    $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $piutang);
                    $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $piutang);
                }else {
                    $objPHPExcel->getActiveSheet()->setCellValue('F' . ($i-1), '');
                    $uraian = $row->URAIAN_USAHA;
                    $piutang = $row->PIUTANG;
                    $totalpiutang += $piutang;
                    $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $uraian);
                    $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $piutang);
                    $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $totalpiutang);
                }
                
                
        }
        //$objPHPExcel->getActiveSheet()->setCellValue("E$i", "=SUM(E2:E".$i.")");
        //$objPHPExcel->getActiveSheet()->setCellValue("F$i", "=SUM(F2:F".$i.")");
        //$objPHPExcel->getActiveSheet()->setCellValue("G$i", "=SUM(G2:G".$i.")");
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&G&C&HKartu Piutang\n!');
        $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objPHPExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // Save it as an excel 2003 file
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="Kartu Piutang.xls"');
        header('Cache-Control: max-age=0');

        $objWriter = IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');

    }
}
?>
